package com.ruoyi.archives.service.impl;

import java.util.List;

import com.ruoyi.archives.domain.CateringUnit;
import com.ruoyi.archives.domain.CateringUnitRecord;
import com.ruoyi.archives.mapper.CateringUnitMapper;
import com.ruoyi.archives.mapper.CateringUnitRecordMapper;
import com.ruoyi.archives.service.ICateringUnitRecordService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * catering_unit_recordService业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class CateringUnitRecordServiceImpl implements ICateringUnitRecordService {
    @Resource
    private CateringUnitRecordMapper cateringUnitRecordMapper;
    @Resource
    private CateringUnitMapper cateringUnitMapper;

    /**
     * 查询catering_unit_record
     *
     * @param id catering_unit_record主键
     * @return catering_unit_record
     */
    @Override
    public CateringUnitRecord selectCateringUnitRecordById(Long id) {
        CateringUnitRecord item = cateringUnitRecordMapper.selectCateringUnitRecordById(id);
        CateringUnit cateringUnit = cateringUnitMapper.selectCateringUnitById(item.getUnitId());
        if (cateringUnit != null) {
            item.setUnitName(cateringUnit.getName());
//                item.getParams().put("unitName", cateringUnit.getName());
        }
        return item;
    }

    /**
     * 查询catering_unit_record列表
     *
     * @param cateringUnitRecord catering_unit_record
     * @return catering_unit_record
     */
    @Override
    public List<CateringUnitRecord> selectCateringUnitRecordList(CateringUnitRecord cateringUnitRecord) {

        if (cateringUnitRecord.getParams() != null) {
            if (cateringUnitRecord.getParams().containsKey("createTimeStart") && cateringUnitRecord.getParams().containsKey("createTimeEnd")) {
                if (cateringUnitRecord.getParams().get("createTimeStart") != null && !"".equals(cateringUnitRecord.getParams().get("createTimeStart"))
                        && cateringUnitRecord.getParams().get("createTimeEnd") != null && !"".equals(cateringUnitRecord.getParams().get("createTimeEnd"))) {
                    cateringUnitRecord.getParams().put("createTimeStart", cateringUnitRecord.getParams().get("createTimeStart") + " 00:00:00");
                    cateringUnitRecord.getParams().put("createTimeEnd", cateringUnitRecord.getParams().get("createTimeEnd") + " 23:59:59");
                }

            }
        }

        List<CateringUnitRecord> cateringUnitRecords = cateringUnitRecordMapper.selectCateringUnitRecordList(cateringUnitRecord);

        cateringUnitRecords.forEach(item -> {
            CateringUnit cateringUnit = cateringUnitMapper.selectCateringUnitById(item.getUnitId());
            if (cateringUnit != null) {
                item.setUnitName(cateringUnit.getName());
//                item.getParams().put("unitName", cateringUnit.getName());
            }
        });
        return cateringUnitRecords;
    }

    /**
     * 新增catering_unit_record
     *
     * @param cateringUnitRecord catering_unit_record
     * @return 结果
     */
    @Override
    public int insertCateringUnitRecord(CateringUnitRecord cateringUnitRecord) {
        cateringUnitRecord.setCreateTime(DateUtils.getNowDate());
        return cateringUnitRecordMapper.insertCateringUnitRecord(cateringUnitRecord);
    }

    /**
     * 修改catering_unit_record
     *
     * @param cateringUnitRecord catering_unit_record
     * @return 结果
     */
    @Override
    public int updateCateringUnitRecord(CateringUnitRecord cateringUnitRecord) {
        cateringUnitRecord.setUpdateTime(DateUtils.getNowDate());
        return cateringUnitRecordMapper.updateCateringUnitRecord(cateringUnitRecord);
    }

    /**
     * 批量删除catering_unit_record
     *
     * @param ids 需要删除的catering_unit_record主键
     * @return 结果
     */
    @Override
    public int deleteCateringUnitRecordByIds(Long[] ids) {
        return cateringUnitRecordMapper.deleteCateringUnitRecordByIds(ids);
    }

    /**
     * 删除catering_unit_record信息
     *
     * @param id catering_unit_record主键
     * @return 结果
     */
    @Override
    public int deleteCateringUnitRecordById(Long id) {
        return cateringUnitRecordMapper.deleteCateringUnitRecordById(id);
    }
}
